/**
 * 通用-关联词
 */
(function($, window, document, undefined) {
    var comChart;

    var SpreadRelateWords = function(el, opts) {
        this.el = el;
        this.defaults = {
            theme: 'macarons',
            chartOption: {
                tooltip: {
                    trigger: 'item',
                    formatter: function(params) {
                        if (params.indicator2) { // is edge
                            return params.indicator2 + ' ' + params.name + ' ' + params.indicator;
                        } else { // is node
                            return params.name
                        }
                    }
                },
                legend: {
                    data: []
                },
                series: [{
                    type: 'chord',
                    ribbonType: false,
                    radius: '60%',
                    itemStyle: {
                        normal: {
                            label: {
                                rotate: true
                            }
                        }
                    },
                    // 使用 nodes links 表达和弦图
                    nodes: [],
                    links: []
                }]
            }
        };
        this.options = $.extend({}, this.defaults, opts);
    };

    SpreadRelateWords.prototype = {
        init: function() {
            var me = this,
                el = me.el;

            if (el.attr('data-initialized') == 'true') {
                return me;
            }

            me.initChart();
            me.bindEvents();


            el.attr('data-initialized', 'true');
            return me;
        },


        /**
         * [bindEvents description]
         * @return {[type]} [description]
         */
        bindEvents: function() {
            var me = this,
                el = me.el;

        },


        /**
         * 格式化数据
         * @param  {[type]} data [description]
         * @return {[type]}      [description]
         */
        formatData: function(originData) {
            var me = this,
                el = me.el,
                chartOption = me.options.chartOption,
                legend = [],
                i, j, len1, len2;

            // 初始化legend
            for (i = 0, len1 = originData.length; i < len1; i++) {
                if (legend.indexOf(originData[i].keyword1) == -1) {
                    legend.push(originData[i].keyword1);
                }

                if (legend.indexOf(originData[i].keyword2) == -1) {
                    legend.push(originData[i].keyword2);
                }
            }
            chartOption.legend.data = legend;

            // 初始化nodes
            for (i = 0, len1 = legend.length; i < len1; i++) {
                chartOption.series[0].nodes.push({
                    name: legend[i]
                });
            }

            // 初始化links
            for (i = 0, len1 = originData.length; i < len1; i++) {
                chartOption.series[0].links.push({
                    source: originData[i].keyword1,
                    target: originData[i].keyword2,
                    weight: originData[i].weight,
                    name: '关联'
                });

                chartOption.series[0].links.push({
                    source: originData[i].keyword2,
                    target: originData[i].keyword1,
                    weight: originData[i].weight,
                    name: '关联'
                });
            }

            return chartOption;
        },

        /**
         * [initChart description]
         * @return {[type]} [description]
         */
        initChart: function() {
            var me = this,
                el = me.el,
                option = {};

            comChart = echarts.init(el[0], me.options.theme);
            option = me.formatData(me.options.data);
            comChart.setOption(option);

            // $.ajax({
            //     url: $.fn.utils.env == 'dev' ? '/keywords/1/spread_relate_words.json' : '',
            //     type: 'GET',
            //     dataType: 'json',
            //     data: {},
            //     success: function(ret) {
            //         if (ret && ret.status) {
            //             var data = ret.data[0].spread_relate_words;

            //             option = me.formatData(me.options.data);
            //             comChart.setOption(option);
            //         } else {

            //         }
            //     },
            //     error: function(ret) {

            //     }
            // });
        }
    };

    $.fn.SpreadRelateWords = function(opts) {
        var me = this,
            el = me.el,
            comSpreadRelateWords;

        // 如果没有被初始化过
        if (!(el && el.length && el.attr('data-initialized') != 'true')) {
            comSpreadRelateWords = new SpreadRelateWords(this, opts);
        }

        return comSpreadRelateWords.init();
    }
})(jQuery, window, document);
